<?php
App::uses('AuthComponent', 'Controller/Component');
class UsersController extends AppController {
	public $components = array('Paginator');
	var $controllerName = "Quản lý người dùng";
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('admin_login','admin_forgot_password');
    }
	
	public function format_price($priceFloat) {
		$symbol = ' VNĐ';
		$symbol_thousand = ',';
		$decimal_place = 2;
		$price = number_format($priceFloat, $decimal_place, '.', $symbol_thousand);
		return $price.$symbol;
		}
		
	public function salary_received($id = null){
		$this->loadModel('Salary');
		$salary = $this->Salary->find('first',array('conditions'=>array('Salary.id'=>$id)));
		$this->set(compact('salary'));
		$this->set('saved',1);
		//debug(date("Y-m-d"));
		if ($this->request->is('post')) {
		//debug($this->request->data);
			$traluong = $this->request->data['Salary']['traluong'];
			$day = date("Y-m-d");
				//debug($phucap);
			$this->User->query("UPDATE  salaries set traluong='".$traluong."',time = '".$day."' where id= '".$id."';");
			if($traluong == 1){
				$this->User->query("INSERT INTO  salaries(users_id,time) value ('".$salary['User']['id']."','".$day."');");
				$this->set('saved',2);
			}else{
				$this->set('saved',0);
			}
		
		}
		
	}
	public function admin_index() {
		$this->User->recursive = 0;
		$s_selected = 0;
		$this->set('users', $this->Paginator->paginate());
		$this->set(compact("s_selected"));
		//debug($this->request->params['admin']);
	}
	public function admin_login() {
		$this->layout = "login";
		if($this->Session->check('Auth.User')){
			$this->redirect(array('action' => 'index'));
		}
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
				$this->redirect($this->Auth->redirectUrl());
			} else {
				$this->Session->setFlash(__('Invalid username or password'));
			}
		}
	}
	
	public function login() {
		$this->layout = "login";
		if($this->Session->check('Auth.User')){
			$this->redirect(array('action' => 'index'));
		}
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				//$this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
				$this->redirect($this->Auth->redirectUrl());
				//debug($this->User->id);
				/*
				if($this->User->id['User']['Groups']['id']=='Admin'){
					$this->Auth->loginRedirect = array('admin' =>true,'controller' => 'users', 'action' => 'index');		
				}else{
					$this->Auth->loginRedirect = array('admin' =>false,'controller' => 'user', 'action' => 'index');
				
				}				
				*/
			} else {
				$this->Session->setFlash(__('Tài khoản hoặc mật khẩu không đúng'));
			}
		}
	}
	public function admin_logout(){
		$this->redirect($this->Auth->logout());

	}
	
	public function index(){
		$this->User->recursive = 0;
		$this->set('users', $this->Paginator->paginate());
		
	}
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been created'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be created. Please, try again.'));
			}
        }
        $groups = $this->User->Groups->find('list');
        $this->set(compact('groups'));
	}
	
	public function add() {
		$this->set('saved', false); 
		$data = array();
		if ($this->request->is('post')) {
			$price = str_replace(',', '', $this->request->data['User']['salary']);
			$data['User']['name']= $this->request->data['User']['name'];
			$data['User']['username']= $this->request->data['User']['username'];
			$data['User']['password']= $this->request->data['User']['password'];
			$data['User']['email']= $this->request->data['User']['email'];
			$data['User']['bird']= $this->request->data['User']['bird'];
			$data['User']['cmt']= $this->request->data['User']['cmt'];
			$data['User']['sex']= $this->request->data['User']['sex'];
			$data['User']['thuongtru']= $this->request->data['User']['thuongtru'];
			$data['User']['tamtru']= $this->request->data['User']['tamtru'];
			$data['User']['phone']= $this->request->data['User']['phone'];
			$data['User']['start']= $this->request->data['User']['start'];
			$data['User']['datework']= $this->request->data['User']['datework'];
			$data['User']['salary']= $price;
			$data['User']['groups_id']= $this->request->data['User']['groups_id'];
			$data['User']['note']= $this->request->data['User']['note'];
			$this->User->create();
			if ($this->User->save($data)) {
				$this->set('saved', true); 
			} else {
				
			}
        }
        $groups = $this->User->Groups->find('list');
        $this->set(compact('groups'));
	}
	
	public function edit($id = null,$slug =null) {
		//debug($this->request->data);
		$this->set('saved', false); //false by default - controls closure of overlay in which this is opened   
		if (!$this->User->exists($this->params['id'])) {
			throw new NotFoundException(__('Không có bản ghi này'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$price = str_replace(',', '', $this->request->data['User']['salary']);
			$data['User']['name']= $this->request->data['User']['name'];
			$data['User']['email']= $this->request->data['User']['email'];
			$data['User']['bird']= $this->request->data['User']['bird'];
			$data['User']['cmt']= $this->request->data['User']['cmt'];
			$data['User']['sex']= $this->request->data['User']['sex'];
			$data['User']['thuongtru']= $this->request->data['User']['thuongtru'];
			$data['User']['tamtru']= $this->request->data['User']['tamtru'];
			$data['User']['phone']= $this->request->data['User']['phone'];
			$data['User']['start']= $this->request->data['User']['start'];
			$data['User']['datework']= $this->request->data['User']['datework'];
			$data['User']['salary']= $price;
			$data['User']['groups_id']= $this->request->data['User']['groups_id'];
			$data['User']['note']= $this->request->data['User']['note'];
			$this->User->create();
			$this->User->id = $this->params['id'];		
			if ($this->User->save($data)) {
				$this->set('saved', true);
				//debug($saved);
				//$this->redirect(array('action' => 'index'));
			}else {$this->Session->setFlash(__('Chưa sửa được. Hãy thử lại'));}
		} else {
			$options = array('conditions' => array('User.' . $this->User->primaryKey => $this->params['id']));
			$this->request->data = $this->User->find('first', $options);
		}
		$groups = $this->User->Groups->find('list');
        $this->set(compact('groups'));
		
	}

	//public function edit($id =null, $slug = null){}

	public function admin_edit($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid User'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The User has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
			$this->request->data = $this->User->find('first', $options);
		}
		$groups = $this->User->Groups->find('list');
		$this->set(compact('groups'));
	}
	
	public function delete($id = null,$slug=null) {
		$this->User->id = $this->params['id'];
		if (!$this->User->exists()) {
			throw new NotFoundException(__('InvalidContent'));
		}
		//$this->request->onlyAllow('post', 'delete');
		if ($this->User->delete($this->params['id'])) {
			$this->Session->setFlash(__('The User has been deleted.'));
		} else {
			$this->Session->setFlash(__('The User could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}

	public function salary(){
		$this->User->recursive = 1;
		$users = $this->User->Salary->find('all',array('conditions'=>array('Salary.traluong'=>0)));
		//$condition = array('Salary.traluong'=>0);
		/*
		$this->Paginator->paginate = array('conditions'=>array('Salary'=>array('Salary.traluong'=>0)));
		$this->set('users',$this->Paginator->paginate());
		debug($this->Paginator->paginate());
		*/
		//debug($users);
		$this->set(compact('users'));
	}
	
	public function admin_delete($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('InvalidContent'));
		}
		//$this->request->onlyAllow('post', 'delete');
		if ($this->User->delete()) {
			$this->Session->setFlash(__('The User has been deleted.'));
		} else {
			$this->Session->setFlash(__('The User could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
	public function admin_forgot_password(){
		$mailer = $this->Components->load("Mailer");
		$mailer->from			= "tranngocthang89@gmail.com";
		$mailer->fromName		= '[Quản trị hệ thống]-Yêu cầu cấp lại mật khẩu';
		$mailer->smtpUserName	= 'tranngocthang89@gmail.com';
		$mailer->smtpPassword	= '************';
		$mailer->to				= '';
		$mailer->toName			= '';
		$mailer->subject		= '';
	}
}
